package com.zzx.boot.servlet.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.boot.entity.table.Topic;
import com.zzx.boot.entity.vo.TopicVo;
import com.zzx.boot.mapper.TopicMapper;
import com.zzx.boot.servlet.TopicService;
import com.zzx.boot.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tan
 * @version 1.0
 * @date 2023/7/8 16:02
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {
    @Override
    public R hotTopic() {
        LambdaQueryWrapper<Topic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Topic::getType,1);
        wrapper.last("limit 0,10");
        List<Topic> list = baseMapper.selectList(wrapper);
        return R.success(list);
    }

    @Override
    public R topicByTopicClass(Long classId, Integer current) {
        Page<Topic> page = new Page<>(current, 10);
        // 根据话题分类查询话题
        LambdaQueryWrapper<Topic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Topic::getTopicClassId,classId);
        wrapper.eq(Topic::getType,1);
        baseMapper.selectPage(page, wrapper);
        List<TopicVo> vos = page.getRecords().stream().map(item -> {
            TopicVo vo = new TopicVo();
            BeanUtils.copyProperties(item, vo);
            vo.setId(item.getId().toString());
            return vo;
        }).collect(Collectors.toList());
        return R.success(vos);
    }

    @Override
    public List<TopicVo> topicSearch(String keyword, Long current) {
        // 根据话题名称和话题描述搜索
        Page<Topic> page = new Page<Topic>(current, 10);
        LambdaQueryWrapper<Topic> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Topic::getTitle,keyword).or().like(Topic::getDesc,keyword);
        baseMapper.selectPage(page,wrapper);
        List<TopicVo> vos = page.getRecords().stream().map(item -> {
            return new TopicVo(item.getId().toString(), item.getTitle(), item.getTitlepic(), item.getDesc());
        }).collect(Collectors.toList());
        return vos;
    }
}
